package it.trekker.statistics;

import it.trekker.dataexchange.PoiRealTimeInfo;
import it.trekker.db.Poi;
import java.util.ArrayList;
import java.util.List;
import android.location.Location;

public class PoiDistanceStatistics extends RealTimeStatistics {

//	private Map<Poi, Double> poiList;
	private List<PoiRealTimeInfo> poiList;
	private List<IDistanceToPoiCallback> distanceToPoiListeners;
	private static PoiDistanceStatistics instance = null;


	public static PoiDistanceStatistics getSharedInstance() {
		if(instance == null)
			instance = new PoiDistanceStatistics();
		
		return instance;
	}
	
	
	public static PoiDistanceStatistics getPrivateInstance() {
		return new PoiDistanceStatistics();
	}
	
	
	private PoiDistanceStatistics() {
		super();

//		poiList = new HashMap<Poi, Double>();
		poiList = new ArrayList<PoiRealTimeInfo>();
		distanceToPoiListeners = new ArrayList<PoiDistanceStatistics.IDistanceToPoiCallback>();
	}

	

	
	
	//************************************************************//
	//******************* IGPSListener methods *******************//
	//************************************************************//
	

	@Override
	public void onLocationChanged(Location location) {
		super.onLocationChanged(location);

//		Iterator it = poiList.entrySet().iterator();
//		while (it.hasNext()) {
//			Map.Entry pairs = (Map.Entry)it.next();
//			double updatedDistance = ((Double)pairs.getValue()).doubleValue() - statisticsManager.getLastPartialDistance();
//			pairs.setValue(new Double(updatedDistance));
//		}
		
		for(PoiRealTimeInfo poi : poiList) {
			double updatedDistance = poi.getDistanceToGoal() + statisticsManager.getLastPartialDistance();
			poi.setDistanceToGoal(updatedDistance);
		}
		
		notifyListeners4DistanceToPoi();
	}

	
	
	
	
	
	
	//*************************************************************//
	//********************** Utility methods **********************//
	//*************************************************************//
	
	
//	public void addPoi(Poi poi, double distance) {
//		if(!poiList.containsKey(poi)) {
//			poiList.put(poi, new Double(distance));
//			
//			notifyListeners4NewPoiAdded(poi);
//		}
//	}
	
	
	public void addPoi(PoiRealTimeInfo poi) {
		if(!poiList.contains(poi)) {
			poiList.add(poi);
			
			notifyListeners4NewPoiAdded(poi);
		}
	}


	public void removePoi(Poi poi) {
		poiList.remove(poi);
	}
	

//	public double getDistanceToPoi(Poi poi) {
//		return poiList.get(poi).doubleValue();
//	}
	public PoiRealTimeInfo getPoiRealTimeInfo(int index) {
		return poiList.get(index);
	}

	
	public List<PoiRealTimeInfo> getPoisList() {
		return poiList;
	}
	

	public void notifyListeners4DistanceToPoi() {
		for(IDistanceToPoiCallback mCallback : distanceToPoiListeners) {
			mCallback.distanceToPoiChanged(poiList);
		}
	}
	
	
//	public void notifyListeners4NewPoiAdded(Poi poi) {
//		for(IDistanceToPoiCallback mCallback : distanceToPoiListeners) {
//			mCallback.newPoiAdded(poi);
//		}
//	}
	public void notifyListeners4NewPoiAdded(PoiRealTimeInfo poi) {
		for(IDistanceToPoiCallback mCallback : distanceToPoiListeners) {
			mCallback.newPoiAdded(poi);
		}
	}
	
	
	public void registerDistanceToPoiCallback(IDistanceToPoiCallback listener) {
		distanceToPoiListeners.add(listener);
	}
	
	
	public void removeDistanceToPoiCallback(IDistanceToPoiCallback listener) {
		distanceToPoiListeners.remove(listener);
	}

	
	@Override
	public void invalidateSharedInstance() {
		PoiDistanceStatistics.instance = null;
	}


	
	

	//*************************************************//
	//**************** Utility classes ****************//
	//*************************************************//
	

	public interface IDistanceToPoiCallback {
		public void newPoiAdded(PoiRealTimeInfo poi);
		public void distanceToPoiChanged(List<PoiRealTimeInfo> distances);
	}
}
